package com.xfhy.wallpaperdemo.live;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.util.Log;

import com.vi.daemon.DaemonNative;
import com.xfhy.wallpaperdemo.DaemonLog;
import com.xfhy.wallpaperdemo.RomUtil;
import com.xfhy.wallpaperdemo.account.AccountHelper;
import com.xfhy.wallpaperdemo.screenmonitor.ScreenMonitorHelper;

import java.io.File;

/* renamed from: nk  reason: default package */
public class nk {
    public Context b;
    public pk.b d;

    /* renamed from: nk$a */
    public class a implements Runnable {
        public a() {
        }

        @Override
        public void run() {
            nk nkVar = nk.this;
            //重启进程时经过这里
            nkVar.a(nkVar.b);
        }
    }

    /* renamed from: nk$b */
    public class b implements Runnable {
        public b() {
        }

        @Override
        public void run() {
            nk.this.a();
        }
    }

    /* renamed from: nk$c */
    public class c implements Runnable {
        public c() {
        }

        @Override
        public void run() {
            AccountHelper.autoSyncAccount(nk.this.b);
        }
    }

    /* renamed from: nk$d */
    public class d implements pk.b {
        public d() {
        }

        @Override
        public void onScreenStatusChanged(boolean z2) {
            if (!RomUtil.isOppo() || z2) {
                nk.this.a();
            } else {
                AccountHelper.cancelSync(nk.this.b);
            }
        }
    }

    /* renamed from: nk$e */
    public class e implements Runnable {

        /* renamed from: nk$e$a */
        public class a implements Runnable {
            public a() {
            }

            @Override
            public void run() {
                DaemonNative.restartProcess();
            }
        }

        public e() {
        }

        @SuppressLint("WrongConstant")
        @Override
        public void run() {
            Process.setThreadPriority(-2);
            DaemonLog.d("startPackageMonitor");
            while (true) {
                try {
                    if ((nk.this.b.getPackageManager().getApplicationInfo(nk.this.b.getPackageName(), 128).flags & 2097152) != 0) {
                        DaemonNative.restartProcess();
                        for (int i = 0; i < 3; i++) {
                            new Thread(new a()).start();
                        }
                    }
                } catch (Throwable th) {
                    DaemonLog.e("getApplicationInfo error", th);
                }
            }
        }
    }

    /* renamed from: nk$f */
    public static final class f {
        @SuppressLint({"StaticFieldLeak"})
        public static nk a = new nk((a) null);
    }

    public /* synthetic */ nk(a aVar) {
        this();
    }

    private void c() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.SCREEN_ON");
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        intentFilter.addAction("android.intent.action.CLOSE_SYSTEM_DIALOGS");
        intentFilter.addAction("android.intent.action.USER_PRESENT");
        intentFilter.addAction("android.intent.action.ACTION_SHUTDOWN");
        this.b.registerReceiver(new DaemonReceiver(), intentFilter);
    }

    /**
     * 是否需要启动 后台线程监控
     */
    private boolean d() {
        //非miui 就不开启
        //高于或等于29 就不开启
        if (!RomUtil.isMiui() || Build.VERSION.SDK_INT >= 29) {
            return false;
        }
        return true;
    }

    private void e() {
        if (d()) {
            Log.d("xfhy_daemon","开启监控");
            new Thread(new e()).start();
        }
    }

    public static nk getInstance() {
        return f.a;
    }

    /*public ok getCallback() {
        return this.c;
    }

    public mk getConfig() {
        return this.a;
    }*/

    public Context getContext() {
        return this.b;
    }

    public void init(Context context/*, mk mkVar, ok okVar*/) {
        this.b = context;
        DaemonLog.d("SyncManager DaemonManager init");
        ProcessHolder.init(context);
        DaemonHelperNk.init(context);
        if (ProcessHolder.IS_MAIN) {
            b();
        }
        if (ProcessHolder.IS_MAIN || ProcessHolder.IS_DAEMON) {
            if (ProcessHolder.IS_MAIN) {
                Log.d("xfhy_daemon", "主进程");
            } else {
                Log.d("xfhy_daemon", "daemon进程");
            }
            //监听各种事件
            c();
            //开启一个线程  while(true) 复活进程
            e();
            //fork进程
            new Thread(new a()).start();
        }
        DaemonHelperNk.startServices(context);
        if ((ProcessHolder.IS_MAIN || ProcessHolder.IS_SERVICE) /*&& this.a.isScreenMonitorEnable()*/) {
            if (ProcessHolder.IS_MAIN) {
                pk.getInstance().addCallback(this.d);
            } else {
                pk.setQueryInterval(1000);
            }
            ScreenMonitorHelper.start();
        }
        //开启JobService,然后开启核心服务
        DaemonJobService.scheduleService(context);
        if (ProcessHolder.IS_MAIN) {
            //3秒同步一下账户
            new Handler(Looper.getMainLooper()).postDelayed(new b(), 3000);
        }
    }

    public nk() {
        this.d = new d();
    }

    private void b() {
        for (String next : DaemonHelperNk.getIndicatorFiles(this.b)) {
            if (next != null && new File(next).delete()) {
                DaemonLog.d("delete indicatorFile success,file=" + next);
            }
        }
    }

    /* access modifiers changed from: private */
    public void a() {
        new Thread(new c()).start();
    }

    /* access modifiers changed from: private */
    public void a(Context context) {
        //重启进程时经过这里
        DaemonLog.d("forkChild,context=" + context);
        String forkName = DaemonHelperNk.getForkName();
        String selfForkLockFile = DaemonHelperNk.getSelfForkLockFile(context);
        String selfForkWaitFile = DaemonHelperNk.getSelfForkWaitFile(context);
        String selfForkIndicatorFile = DaemonHelperNk.getSelfForkIndicatorFile(context);
        String selfForkWaitIndicatorFile = DaemonHelperNk.getSelfForkWaitIndicatorFile(context);
        DaemonLog.d("===============forkChild log start ==============");
        DaemonLog.d("forkChild,forkName=" + forkName);
        DaemonLog.d("forkChild,forkLockFile=" + selfForkLockFile);
        DaemonLog.d("forkChild,forkWaitFile=" + selfForkWaitFile);
        DaemonLog.d("forkChild,forkIndicatorFile=" + selfForkIndicatorFile);
        DaemonLog.d("forkChild,forkWaitIndicatorFile=" + selfForkWaitIndicatorFile);
        DaemonLog.d("===============forkChild log end==============");
        DaemonNative.forkChild(forkName, selfForkLockFile, selfForkWaitFile, selfForkIndicatorFile, selfForkWaitIndicatorFile);
    }
}
